﻿Imports System.Windows.Forms

Public Class dlgTranslationManagement

    Private _nls As Localizer
    Private _entries As New List(Of NlsEntry)



    Sub New(contextCaption As String, nls As Localizer)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _nls = nls
        Me.Text &= contextCaption
    End Sub

    Protected Overrides Sub OnLoad(e As System.EventArgs)
        MyBase.OnLoad(e)
        If _nls Is Nothing Then Exit Sub
        For Each kvp In _nls
            _entries.Add(New NlsEntry() With {.NlsKey = kvp.Key, .NlsText = kvp.Value, .LanguageID = _nls.LanguageID, .LanguageName = _nls.LanguageName, .Changed = False})
        Next
        colLanguageName.Visible = Not String.IsNullOrEmpty(_nls.LanguageName)
        colLanguageID.Visible = String.IsNullOrEmpty(_nls.LanguageName)
        grdNls.DataSource = _entries
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If _entries.Any(Function(entry) entry.Changed) Then
            If MessageBox.Show("You have made modifications. Are you sure you wish to exit without saving?", Me.Text, MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
                Exit Sub
            End If
        End If
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub


    Private Sub btnOk_Click(sender As System.Object, e As System.EventArgs) Handles btnOk.Click
        Dim changed = _entries.Where(Function(entry) entry.Changed)
        For Each change In changed
            _nls.UpdateTranslation(change.NlsKey, change.NlsText)
            change.Changed = False
        Next
        MessageBox.Show("Changes have been saved", Me.Text, MessageBoxButtons.OK)
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Class NlsEntry


        Public Property Changed As Boolean

        Public Property NlsKey As String

        Public Property NlsText As String
            Get
                Return _nlsText
            End Get
            Set(value As String)
                If _nlsText <> value Then

                End If
                _nlsText = value
                Changed = True
            End Set
        End Property
        Private _nlsText As String


        Public Property LanguageName As String

        Public Property LanguageID As Integer


    End Class

End Class
